package com.softwinner.awbt.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.softwinner.awbt.R;
import com.softwinner.awbt.common.BTApplication;
import com.softwinner.awbt.common.MessageKey;
import com.softwinner.awbt.controller.BTController;
import com.softwinner.awbt.dao.Contact;
import com.softwinner.awbt.dao.ContactDao;
import com.softwinner.awbt.utils.LOG;

import java.util.List;

public class InComingActivity extends Activity implements OnClickListener {
    private ImageView iv_connect;
    private ImageView iv_hangup;
    private TextView tv_incoming_phonenumber;
    private String incomingNumber;

    private static Handler hand = null;
    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MessageKey.MSG_DISCONNECT:
                    finish();
                    break;
                case MessageKey.MSG_INCOMING_HANGUP:
                case MessageKey.MSG_HANGUP_PHONE:
                    finish();
                    break;
                case MessageKey.MSG_INCOMING_CONNECT:
                    LOG.i("InComingActivity----接通  incomingNumber=" + incomingNumber);
                    if (incomingNumber != null) {
                        LOG.i("来电了，你到底接没接");
                        connectInComing(incomingNumber);
                    }
                    break;
            }
        }

    };

    public static Handler getHandler() {
        return hand;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LOG.d("InComingActivity   onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_incoming);
        Intent intent = getIntent();
        incomingNumber = intent.getStringExtra("incomingNumber");
        tv_incoming_phonenumber = (TextView) findViewById(R.id.tv_incoming_phonenumber);
        tv_incoming_phonenumber.setText(incomingNumber);
        iv_connect = (ImageView) findViewById(R.id.iv_connect);
        iv_hangup = (ImageView) findViewById(R.id.iv_hangup);
        iv_connect.setOnClickListener(this);
        iv_hangup.setOnClickListener(this);
        List<Contact> contacts = BTApplication.getInstance().getDaoSession().getContactDao()
                .queryBuilder().where(ContactDao.Properties.Number.eq(incomingNumber)).list();
        if (contacts.size() > 0) {
            tv_incoming_phonenumber.setText(contacts.get(0).getName());
        } else {
            tv_incoming_phonenumber.setText(incomingNumber);
        }
        hand = handler;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_connect:
                try {
                    BTController.getInstance().phoneAnswer();
                } catch (RemoteException e) {
                    LOG.e("RemoteException " + e.getMessage());
                }
                break;
            case R.id.iv_hangup:
                hangupInComing();
                break;

        }
    }

    private void hangupInComing() {
        try {
            BTController.getInstance().phoneHangUp();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        finish();
    }

    private void connectInComing(String incomingNumber2) {
        try {
            BTController.getInstance().phoneAnswer();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(this, CallActivity.class);
        intent.putExtra("incomingNumber", incomingNumber2);
        intent.putExtra("isConnect", true);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onResume() {
        LOG.d("InComingActivity-----onResume---");
        super.onResume();
    }

    @Override
    protected void onStop() {
        LOG.d("InComingActivity-----onStop---");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        LOG.d("InComingActivity-----onDestroy---");
        super.onDestroy();
    }
}
